/*
 * CopyPointer.h
 *
 *  Created on: 17/nov/2010
 *      Author: computer
 */

#ifndef COPYPOINTER_H
#define COPYPOINTER_H

template<class T> class CopyPointer {
public:
	explicit CopyPointer(T* _ptr) : ptr(_ptr) { }
	explicit CopyPointer() : ptr(0) { }

	CopyPointer(const CopyPointer<T>& toCopy) {
		if (toCopy.ptr) ptr = new T(*toCopy.ptr);
		else ptr = 0;
	}

	CopyPointer<T>& operator=(const CopyPointer<T>& toCopy) {
		if (ptr) {
			delete ptr;
		}
		if (toCopy.ptr) ptr = new T(*toCopy.ptr);
		return *this;
	}

	~CopyPointer() {
		if (ptr) {
			delete ptr;
		}
	}

	T* operator->() const {
		return ptr;
	}

	T& operator*() const {
		return *ptr;
	}

	T* get() const { return ptr; }

	void reset(T* _ptr) {
		if (ptr) delete ptr;
		ptr = _ptr;
	}

	void set(T* _ptr) {
		ptr = _ptr;
	}

	bool isNull() const { return ptr == 0; }

private:
	T* ptr;
};

#endif

